<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('告警信息列表')" />
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="post-form">
					<div class="select-list">
						<ul>
							<li class="select-time">
								<label>告警时间： </label>
								<input type="text" data-type="datetime" data-format="yyyy-MM-dd HH:mm:ss" style="width: 140px" class="time-input" id="beginTime" placeholder="开始时间" name="beginTime"/>
								<span>-</span>
								<input type="text" data-type="datetime" data-format="yyyy-MM-dd HH:mm:ss" style="width: 140px" class="time-input" id="endTime" placeholder="结束时间" name="endTime"/>
							</li>
							<li>
								告警等级：
								<select id="level" name="level" th:with="type=${@dict.getType('alarm_level')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
								</select>
							</li>
							<li>
								告警类型：
								<select name="ruleType" th:with="type=${@dict.getType('alarm_rule_list')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
								</select>
							</li>
							<li>
								机器处理：
								<select name="machineHandle" th:with="type=${@dict.getType('enum_yes_no')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
								</select>
							</li>
							<li>
								人工处理：
								<select name="artificialHandle" th:with="type=${@dict.getType('enum_yes_no')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
								</select>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>

			<div class="btn-group-sm" id="toolbar" role="group">
				<!--<a class="btn btn-success" onclick="$.operate.add()">
	                <i class="fa fa-plus"></i> 新增
	            </a>-->
				<a class="btn btn-primary multiple disabled" onclick="handleAll()">
		            <i class="fa fa-check-square-o"></i> 处理
		        </a>
				<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()">
		            <i class="fa fa-remove"></i> 删除
		        </a>
				<a class="btn btn-warning" onclick="$.table.exportExcel()">
		            <i class="fa fa-download"></i> 导出
		        </a>
	        </div>

	        <div class="col-sm-12 select-table table-striped">
			    <table id="bootstrap-table"></table>
			</div>
		</div>
	</div>
	<th:block th:include="include :: footer" />
	<script th:src="@{/ajax/libs/datapicker/bootstrap-datetimepicker.js}"></script>
	<script th:inline="javascript">
		var alarmLevelDatas = [[${@dict.getType('alarm_level')}]];
		var ruleTypeDatas = [[${@dict.getType('alarm_rule_list')}]];
		var handlingDatas = [[${@dict.getType('enum_yes_no')}]];
		var selectedLevel = [[${level}]]
		var prefix = ctx + "system/alarm";

		$(function() {
			$("#level").val(selectedLevel);
			queryList();
		});

		function queryList() {
			var options = {
				url: prefix + "/list",
				createUrl: prefix + "/add",
				updateUrl: prefix + "/handle/{ids}",
				removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
				sortName: "createTime",
				sortOrder: "desc",
				modalName: "告警数据",
				columns: [{
					checkbox: true
				},
                {
                    field: 'id',
                    title: '编号',
                    sortable: true,
                    visible: false
                },
                {
                    field: 'ruleId',
                    title: '告警规则编号',
					sortable: true,
					visible: false
                },
				{
					field: 'alarmObject',
					title: '告警对象'
				},
                {
                    field: 'level',
                    title: '告警等级',
                    sortable: true,
					formatter: function (value, row, index) {
						return $.table.selectDictLabel(alarmLevelDatas, value);
					}
                },
				{
					field: 'ruleType',
					title: '规则类型',
					formatter: function (value, row, index) {
						return $.table.selectDictLabel(ruleTypeDatas, value);
					}
				},
				{
					field: 'nodeCode',
					title: '主机编号',
					visible: false
				},
				{
					field: 'ip',
					title: 'IP地址',
					visible: false
				},
				{
					field: 'createTime',
					title: '创建时间',
					sortable: true
				},
				{
					field: 'machineHandle',
					title: '机器是否处理',
					visible: false,
					formatter: function (value, row, index) {
						return $.table.selectDictLabel(handlingDatas, value);
					}
				},
				{
					field: 'machineHandleTime',
					title: '机器处理时间',
					visible: false,
					formatter: function (value, row, index) {
						if (value != 0) {
							var datetime = new Date();
							datetime.setTime(value);
							return datetime.format('yyyy-MM-dd hh:mm:ss');
						} else {
							return $.table.selectDictLabel(handlingDatas, value);
						}
					}
				},
				{
					field: 'artificialHandle',
					title: '人工是否处理',
					visible: false,
					formatter: function (value, row, index) {
						return $.table.selectDictLabel(handlingDatas, value);
					}
				},
				{
					field: 'artificialHandleTime',
					title: '人工处理时间',
					visible: false,
					formatter: function (value, row, index) {
						if (value != 0) {
							var datetime = new Date();
							datetime.setTime(value);
							return datetime.format('yyyy-MM-dd hh:mm:ss');
						} else {
							return $.table.selectDictLabel(handlingDatas, value);
						}
					}
				},
				{
					field: 'alarmContent',
					title: '告警内容'
				},
				{
					field: 'artificialMessage',
					title: '处理留言'
				},
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
						if (row.artificialHandle == 1) {
							actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
						} else if (row.machineHandle == 1) {
							actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="handle(\'' + row.id + '\')"><i class="fa fa-check-square-o"></i>处理</a> ');
						}
                        return actions.join('');
                    }
                }]
			};
			$.table.init(options);
		}

		/* 处理告警 */
		function handle(id) {
			var url = prefix + '/handle/' + id;
			$.modal.open("处理告警", url, '800', '300');
		}

		function handleAll() {
			table.set();
            var rows = $("#bootstrap-table").bootstrapTable('getSelections');
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
			let actions = [];
			for (let i = 0; i < rows.length; i++) {
				r = rows[i];
				console.log(r)
				actions.push(r.id);
			}
            console.log(actions.join(","))
            var url = table.options.updateUrl.replace("{ids}", actions.join(","));
            $.modal.open("修改" + table.options.modalName, url, '800', '300');
		}

	</script>
</body>
</html>